package com.tm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.dto.SetmealDTO;
import com.tm.dto.SetmealDTOO;
import com.tm.entity.DishEntity;
import com.tm.entity.DishFlavorEntity;
import com.tm.entity.SetmealDishEntity;
import com.tm.entity.SetmealEntity;
import com.tm.mapper.SetmealMapper;
import com.tm.service.SetmealDishService;
import com.tm.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huangp
 * @create 2023-04-06 15:57
 */

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, SetmealEntity> implements SetmealService {

    @Resource
    private  SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public Page<SetmealEntity> pageList(Integer page, Integer pageSize, SetmealDTO setmealDTO) {
       Page<SetmealEntity> page2 = new Page<>(page,pageSize);
        QueryWrapper<SetmealEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("a.is_deleted",0);
        queryWrapper.eq("c.type",2);
        queryWrapper.like(StringUtils.isNotBlank(setmealDTO.getName()),"a.name",setmealDTO.getName());
        Page<SetmealEntity> page6 =setmealMapper.pageList(page2,queryWrapper);
        return page6;
    }

    @Transactional
    public void addSetmeal(SetmealDTOO setmealDTOO) {
        //新增信息
        this.save(setmealDTOO);

        List<SetmealDishEntity> setmealDishes = setmealDTOO.getSetmealDishes();
        setmealDishes.stream().map(x ->{
            x.setSetmealId(setmealDTOO.getId());
            return x;
        }).collect(Collectors.toList());

        //保存套餐=菜品信息的关联关系
         setmealDishService.saveBatch(setmealDishes);
    }



    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {

        //查询套餐状态，确定是否删除数据
        LambdaQueryWrapper<SetmealEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealEntity::getId,ids);
        queryWrapper.eq(SetmealEntity::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0){
            throw new RuntimeException("套餐正在售卖NE，不能删除");
        }

        //先删除表里的数据
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDishEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.in(SetmealDishEntity::getSetmealId,ids);

        setmealDishService.remove(lambdaQueryWrapper);

    }

    @Override
    public SetmealDTOO getByIfwithDish(Long id) {
        //查询菜品基本信息
        SetmealEntity setmealEntity = this.getById(id);

        SetmealDTOO setmealDTOO = new SetmealDTOO();
        BeanUtils.copyProperties(setmealEntity,setmealDTOO);

        LambdaQueryWrapper<SetmealDishEntity> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(SetmealDishEntity::getSetmealId,setmealEntity.getId());

        List<SetmealDishEntity> list = setmealDishService.list(queryWrapper);

        setmealDTOO.setSetmealDishes(list);

        return setmealDTOO;

    }

    @Override
    public void setmealStatusByStatus(Integer status, List<Long> ids) {
        UpdateWrapper<SetmealEntity> wrapper = new UpdateWrapper<>();
        wrapper.in("id",ids);
        SetmealEntity setmealEntity = new SetmealEntity();
        setmealEntity.setStatus(status);
        super.update(setmealEntity,wrapper);
    }

    @Override
    public void updateWithSetmeal(SetmealDTOO setmealDTOO) {

        //更新dish表基本信息
        this.updateById(setmealDTOO);
        //清理当前菜品对应口味数据--dish_flavor表的delete操作
        LambdaQueryWrapper<SetmealDishEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDishEntity::getId, setmealDTOO.getId());
        setmealDishService.remove(queryWrapper);

        //添加当前提交过来的口味数据--dish_flavro表的insert操作
        List<SetmealDishEntity> setmealDishes = setmealDTOO.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((x) -> {
            x.setSetmealId(setmealDTOO.getId());
            return x;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);


    }


}
